// pages/index/index.js
import qqmapwx from '../../utils/qqmap-wx-jssdk.min.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        city:"武汉",
        hasLocation:false,
    },
    onLoad: function (options) {
        let that=this;
        const qqmapsdk = new qqmapwx({
            // 使用你在腾讯地图应用生成的key
            key: '2JSBZ-6PDKF-DJIJ3-N6SUN-TAEN5-WXFQP'
          });
          // 小程序会弹窗提示授权后才会执行
          wx.getLocation({
            type: 'wgs84',
            success:(res)=>{
              //得到经纬度
              qqmapsdk.reverseGeocoder({
                location: {
                  latitude: res.latitude,
                  longitude: res.longitude
                },
                //成功后的回调
                success: (r) => {
                    let userCity=String(r.result.address_component.city);
                    console.log("输出用户当前的城市：",r.result.address_component);
                    if(userCity.indexOf("市")!=-1){
                        userCity=userCity.substring(0,userCity.length-1);
                    }
                     app.globalData.locationCity=userCity;
                     that.setData({
                         city:userCity
                     });
                },
                fail: function (res) {
                  console.log(res);
                }
              });
            }
          });
    },
    onShow:function(){
        if(app.globalData.userCity){
            this.setData({
                city:app.globalData.userCity
            });
        }
    },
    changeCity:function(){
        wx.navigateTo({
          url: '/pages/city/city',
        })
    },
})